package com.java.controller;

import cn.hutool.crypto.SecureUtil;
import com.java.config.exceptioin.YyghException;
import com.java.config.result.Result;
import com.java.model.hosp.HospitalSet;
import com.java.model.vo.HospitalSetQueryVo;
import com.java.model.vo.SignInfoVo;
import com.java.service.HospitalSetService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //
    @GetMapping("/list")
    public Result getList(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //删除医院设置
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        boolean remove = hospitalSetService.removeById(id);
        if(remove){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //医院设置条件查询分页
    @PostMapping("/pageList/{pageNum}/{pageSize}")
    public Result pageList(@PathVariable Long pageNum,
                           @PathVariable Long pageSize,
                           @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){

        Result result = hospitalSetService.selectPage(pageNum, pageSize, hospitalSetQueryVo);
        return result;
    }

    //添加医院设置
    @PostMapping("/saveHopitalSet")
    public Result saveHopitalSet(@RequestBody HospitalSet hospitalSet){
        //在保存前设置医院的签名 和 状态
        Random random = new Random();
        String data = random.nextInt(1000)+""+System.currentTimeMillis();
        String md5 = SecureUtil.md5(data);
        hospitalSet.setSignKey(md5);
        hospitalSet.setStatus(1);
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }


    //根据id获取医院设置
    @GetMapping("getHosp/{id}")
    public Result getHospitalById(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //根据id修改医院设置
    @PostMapping("/updateHosp")
    public Result updateHosp(@RequestBody HospitalSet hospitalSet){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update){
            return Result.ok();
        }
        return Result.fail();
    }

    //批量修改医院设置
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHosp(@RequestBody List<Long> ids){
        try {
            hospitalSetService.removeByIds(ids);
        } catch (Exception e) {
            throw new YyghException("失败",201);
        }
        return Result.ok();
    }

    //修改医院设置的状态
    @PutMapping("/lock/{id}/{status}")
    public Result updateHospByStatus(@PathVariable("id") Long id,
                                     @PathVariable("status") Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @PutMapping("/sendKey/{id}")
    public Result lockHospitalSet(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }

    //获取医院签名信息
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(@PathVariable String hoscode){
        SignInfoVo signInfoVo = hospitalSetService.getSignInfoVo(hoscode);
        return signInfoVo;
    }
}
